<?php

require_once 'Pipes/Config.php';
require_once 'Pipes/View.php';

class Pipes_Generator_Create_View
{
	private $_projectPath = '';
	private $_view = null;

	public function __construct($projectPath) {
		$this->_projectPath = $projectPath;

		$config = new Pipes_Config(array('projects' => array('PipesCore')));
		$this->_view = new Pipes_View($config, 'GeneratorTemplates');
		$this->_view->project = basename($this->_projectPath);
		$this->_view->generatingClass = __CLASS__;
	}
	public function create($name) {
		$this->_view->name = $name;
		$content = array(
			'add'	=> str_replace('<\?php', '<?php', $this->_view->render('view/add')->toString()),
			'edit'   => str_replace('<\?php', '<?php', $this->_view->render('view/edit')->toString()),
			'browse' => str_replace('<\?php', '<?php', $this->_view->render('view/browse')->toString()),
			'remove' => str_replace('<\?php', '<?php', $this->_view->render('view/remove')->toString()),
			'dashboard' => str_replace('<\?php', '<?php', $this->_view->render('view/dashboard')->toString()),
		);

		$viewPath = "{$this->_projectPath}/Views/{$name}/";
		mkdir($viewPath);
		foreach ($content as $fileName => $fileContent) {
			$fp = fopen("{$viewPath}{$fileName}.php", 'x+');
			fwrite($fp, $fileContent);
			fclose($fp);
		}
	}
}
